<?php
/**
 * vim: tabstop=4
 * 
 * @license		http://www.gnu.org/licenses/gpl.html GPL Version 3
 * @author		Ian Moore <imooreyahoo@gmail.com>
 * @copyright	Copyright (c) 2011 Ian Moore
 * @version		$Id: processlist.inc 76 2011-12-10 22:19:28Z imooreyahoo@gmail.com $
 *
 * This file is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This file is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this file. If not, see <http://www.gnu.org/licenses/>.
 */
require_once("openmediavault/object.inc");
require_once("openmediavault/error.inc");
require_once("openmediavault/util.inc");
require_once("openmediavault/rpc.inc");

class ProcessListRpc extends OMVRpc {

	public function __construct() {
		$this->methodSchemata = array(
            "getProcessList" => array(
                '{"type":"integer"}', // start
                '{"type":"integer"}', // count
                '{'.$GLOBALS['OMV_JSONSCHEMA_SORTFIELD'].'}', // sortField
                '{'.$GLOBALS['OMV_JSONSCHEMA_SORTDIR'].'}' // sortDir
            )
		);

	}

    /**
     * Verify that the current user is an admin, and validate method args
     */
    private function _validate($mname='',$args=array()) {

        // Check permissions
        $this->validateSession();
        if (!$this->hasRole(OMV_ROLE_ADMINISTRATOR)) {
            throw new OMVException(OMVErrorMsg::E_RPC_SERVICE_INVALID_PERMISSION);
        }
        $this->commitSession();

        // Check incoming data
        if($mname)
            $this->validateParams($mname, $args);
    }


	/**
	 * Return a list of active leases
	 * @param $start integer start point in paging list
	 * @param $count integer number of objects to return in paged list
	 * @param $sortField string field to sort on
	 * @param $sortDir integer sort direction
	 * @return array list of leases
	 */
    public function getProcessList($start, $count, $sortField, $sortDir) {

        // Validation
        $this->_validate(__METHOD__,func_get_args());


		// Create '/etc/cups.conf' file
		$cmd = 'sudo ps axww -o pid,user,%cpu,%mem,stat,args 2>&1';
		OMVUtil::exec($cmd, $output, $result);
		if ($result !== 0) {
			throw new OMVException(OMVErrorMsg::E_EXEC_FAILED,
			  $cmd, implode("\n", $output));
		}
				
		// Get column names
		$cols = array_map(function($a) {
			return preg_replace("/[^A-Z]+/",'',$a);
		}, preg_split('/\s+/', trim(array_shift($output))));
		
		$split_limit = count($cols);
		
		$objects = array();
		foreach($output as $line) {
			if(trim($line) == '') continue;
			$objects[] = array_combine($cols, preg_split('/\s+/', trim($line), $split_limit));
		}

        // Filter result
        return $this->applyFilter($objects, $start, $count, $sortField, $sortDir);

    }
}

